* This file lists all the do-files used to create the data and produce the analysis reported in 'Saving Lives by Tying Hands'
* These files require the use of proprietry hospital data. These data can be obtained from NHS Digital. See the Readme document in this folder for more details on this application process.
* Once you have obtained the data - and placed it in the 'sourcedata' folder below - change the globals below to reflect the correct filepaths on your machine to run the files
* The do-file lists below where files must be ran in a particular order (e.g. data creation, Appendix F).


global dofiles ""
global sourcedata ""  /* Raw Inpatient and ED data should be placed here */
global saveddata "" /* Stores data created in the files below */
global inputdata "" /* Publicly available data required to run the files - these are currently saved in the main folder (i.e. dofiles and inputdata global should be the same unless these are moved) */
global results "" /* Stores all results */

global temp_inpat "" /* Temporary files when creating inpatient census */
global temp_ae "" /* Temporary files when creating ED census */

** Construct data for main analyses (must be run in order)
************************************************************

* 1) Build ED (A&E) data for 2011-12 and 2012-13
do "$dofiles/01_create_ed_data.do"

* 2) Build inpatient data for 2011-12 and 2012-13, with costs of treatment
do "$dofiles/02_create_inpatient_data.do"


* 3) Build previous health data for demographic tests, using 2010-11 inpatient data
do "$dofiles/03_create_2010_demovars.do"

* 4) Put ED and inpatient dataset together and clean - note I need to still edit this to make it fully ready for analysis (rather than at the start of the analysis files?)
do "$dofiles/04_create_analysis_dataset.do"


** Main results (can be run in any order)
**********************************************

* Analysis and output for Table 1
do "$dofiles/analysis_make_table1.do"

* Analysis and output for Table 2
do "$dofiles/analysis_make_table2.do"

* Analysis and output for Table 3
do "$dofiles/analysis_make_table3.do"

* Analysis and output for Figures 1, 2, 3, Appendix Figures A2, A3, C1, C2
do "$dofiles/analysis_make_charts.do"

** Appendix A analysis and outputs 
*************************************

** Figure A1
do "$dofiles/AppA_FigA1.do" // Needs proprietry data from US hospital to run, received by private correspondance with David Chan in October 2016.

** Table A1 - run 4 do-files, each has a separate bootstrap for different value of w- (col3 is the baseline results): outputs results for each column in a separate excel file
do "$dofiles/AppA_tableA1_col1.do" // w- = 160
do "$dofiles/AppA_tableA1_col2.do" // w- = 170
do "$dofiles/AppA_tableA1_col4.do" // w- = 190
do "$dofiles/AppA_tableA1_col5.do" // w- = 200

** Table A2 - run 4 do-files, 3 of which run a separate bootstrap for different polynomial order (col3 is the baseline results): outputs results for each column in a separate excel file
*** NOTE: AppA_make_polystar.do must be run before AppA_tableA2_col4.do
do "$dofiles/AppA_tableA2_col1.do" // porder = 6
do "$dofiles/AppA_tableA2_col2.do" // porder = 8
do "$dofiles/AppA_make_polystar.do" // creates porder=* for each variable
do "$dofiles/AppA_tableA2_col4.do" // porder = *

** Table A3 - NOTE: Must run AppF_make_diagnosis_data.do (see below) before running this file to obtain results
do "$dofiles/AppA_tableA3.do"

** Table A4
do "$dofiles/AppA_tableA4.do"

** Table A5
do "$dofiles/AppA_tableA5.do"

* Appendix B analysis and outputs
***********************************
do "$dofiles/AppB_tables.do"

* Appendix C outputs (Table C1 and C2) produced in analysis_make_charts.do
****************************************************************************

* Appendix D data creation, analysis and outputs (Fig D1 and D2)
******************************************************************

**Figure D1
do "$dofiles/AppD_make_FigD1_data.do" // Sets up data used in analysis for Figure D1
do "$dofiles/AppD_FigD1.do" // Figure D1

** Figure D2
do "$dofiles/AppD_make_FigD2_data.do" // Sets up data used in analysis for Figure D2 [CHECKING THIS WORKS AT THE MOMENT]
do "$dofiles/AppD_FigD2.do" // Figure D2


* Appendix E data creation, analysis and outputs (Fig E1-5)
**************************************************************
do "$dofiles/AppE_Fig1to3.do" // Figures E1-3
do "$dofiles/AppE_Fig4to5.do" // Figures E4-5

* Appendix F data creation, analysis and outputs (Figs F1-3, Table F1) - these should be run in the order specified below 
***************************************************************************************************************************************
do "$dofiles/AppF_make_polystar_diag.do" // make optimal polynomials by diagnosis group 
do "$dofiles/AppF_make_diagnosis_data.do" // make diagnosis dataset for heterogeneity by diagnosis analysis
do "$dofiles/AppF_make_inpatient_census.do" // make inpatient census
do "$dofiles/AppF_make_polystar_crowd.do" // make optimal polynomials by crowding group
do "$dofiles/AppF_make_crowding_data.do" // make crowding dataset for heterogeneity by crowding analysis
do "$dofiles/AppF_analysis.do" // Produces Figures F1, F2, F3 and Table F1

